﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace ChoViet.Model
{
    public class ArticleCategoryManager
    {
        public static IList<SelectListItem> BuiltCombobox(int currentId)
        {

            IList<ArticleCategory> list = GetAll(0);
            IList<ArticleCategory> l = ListTree(list);
            string space = string.Empty;
            if (l != null)
            {
                IList<SelectListItem> sList = new List<SelectListItem>();
                foreach (var c in l)
                {
                    SelectListItem item = new SelectListItem();
                    if (c.ParentCategory == null)
                    {
                        space += " ";
                    }
                    else if (c.ParentCategory != null)
                    {
                        space += " - - - -";
                    }

                    item.Text = space + c.Name;
                    item.Value = c.ID.ToString();
                    item.Selected = c.ID == currentId ? true : false;
                    sList.Add(item);
                    space = string.Empty;
                }
                return sList;
            }

            return new List<SelectListItem>();
        }

        public static IList<ArticleCategory> GetAll(int parentid)
        {
            try
            {
                ChoVietDataContext db = new ChoVietDataContext();
                IList<ArticleCategory> l = new List<ArticleCategory>();
                var list = (from o in db.ArticleCategories where (parentid > 0 ? (parentid == null ? o.ParentID == null : o.ParentID.Value == parentid) : (1 == 1)) select o).ToList();

                return list;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public static IList<ArticleCategory> ListTree(IList<ArticleCategory> list)
        {
            IList<TreeNodeCat> tree = BuildTree(list);
            IList<ArticleCategory> l = new List<ArticleCategory>();
            ListTree(tree, l);
            return l;
        }

        public static void ListTree(IList<TreeNodeCat> listNodes, IList<ArticleCategory> listResult)
        {
            foreach (TreeNodeCat _node in listNodes)
            {
                listResult.Add(_node.Nodevalue);
                ListTree(_node.ChildNodes, listResult);
            }
        }

        public static IList<TreeNodeCat> BuildTree(IList<ArticleCategory> list)
        {
            IList<ArticleCategory> roots = GetRoot(list);
            IList<TreeNodeCat> ret = new List<TreeNodeCat>();
            foreach (ArticleCategory _obj in roots)
            {
                TreeNodeCat _node = new TreeNodeCat(_obj);
                _node.BuildChilNodes(list);
                ret.Add(_node);
            }
            return ret;
        }

        public static IList<ArticleCategory> GetRoot(IList<ArticleCategory> list)
        {
            IList<ArticleCategory> root = new List<ArticleCategory>();
            foreach (ArticleCategory objCheck in list)
            {
                if (objCheck.ParentID == null || objCheck.ParentID <= 0 || objCheck.ID == objCheck.ParentID)
                {
                    root.Add(objCheck);
                }
            }
            return root;
        }

        public class TreeNodeCat
        {
            private ArticleCategory _Nodevalue = null;
            public TreeNodeCat(ArticleCategory mNodeValue)
            {
                _Nodevalue = mNodeValue;
            }
            public ArticleCategory Nodevalue
            {
                get { return _Nodevalue; }
                set { _Nodevalue = value; }
            }

            private List<TreeNodeCat> _childNodes = new List<TreeNodeCat>();
            public List<TreeNodeCat> ChildNodes
            {
                get { return _childNodes; }
                set { _childNodes = value; }
            }
            public int NodeId
            {
                get { return (Nodevalue == null) ? 0 : Nodevalue.ID; }
            }
            public string NodeName
            {
                get { return (Nodevalue == null) ? "" : Nodevalue.Name; }
            }

            public void BuildChilNodes(IList<ArticleCategory> ListNodes)
            {
                foreach (ArticleCategory childObj in ListNodes)
                {
                    if (childObj.ParentID == Nodevalue.ID && childObj.ParentID != childObj.ID)
                    {
                        TreeNodeCat _node = new TreeNodeCat(childObj);
                        _node.BuildChilNodes(ListNodes);
                        this.ChildNodes.Add(_node);
                    }
                }
            }

            private bool isTerminalNode
            {
                get { return ChildNodes.Count <= 0; }
            }
            public static ArticleCategory Find(TreeNodeCat _node, int catId)
            {
                if (_node.Nodevalue.ID == catId) return _node.Nodevalue;
                else
                    foreach (TreeNodeCat _childNode in _node.ChildNodes)
                    {
                        ArticleCategory _obj = Find(_childNode, catId);
                        if (_obj != null) return _obj;
                    }

                return null;
            }
            public static TreeNodeCat FindNode(TreeNodeCat _node, int Id)
            {
                if (_node.Nodevalue.ID == Id) return _node;
                else
                    foreach (TreeNodeCat _childNode in _node.ChildNodes)
                    {
                        TreeNodeCat _obj = FindNode(_childNode, Id);
                        if (_obj != null) return _obj;
                    }

                return null;
            }
            public static TreeNodeCat FindListNode(IList<TreeNodeCat> _nodes, int Id)
            {
                foreach (var obj in _nodes)
                {
                    var item = FindNode(obj, Id);
                    if (item != null) return item;
                }
                return null;
            }
        }
    }
}
